package com.chuangan.releasetask.quartz;

import com.chuangan.releasetask.Utils.QuartzUtils;
import com.chuangan.releasetask.dto.StepDTO;
import com.chuangan.releasetask.enums.TaskStatusEnum;
import com.chuangan.releasetask.mapper.TaskMapper;
import com.chuangan.releasetask.pojo.TaskMaster;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @Author: zihua
 * @Date: 2020/8/13 16:02
 * @Version 1.0
 */
@Component
public class QuartzExecute {

    @Autowired
    private  TaskMapper taskMapper;

    private static QuartzExecute quartzExecute;

    @PostConstruct
    public void init(){
        quartzExecute = this;
        quartzExecute.taskMapper = this.taskMapper;
    }

    public static void auditDeadlineQuartz(String task) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        List<TaskMaster> auditTaskTaskMasters = quartzExecute.taskMapper.selectAuditTask();
        Date auditDeadline = new Date();
        if(auditTaskTaskMasters.size()>0){
            for(TaskMaster taskMaster: auditTaskTaskMasters){
                auditDeadline.setTime(taskMaster.getCreateTime().getTime()+taskMaster.getAuditDeadline()*60*60*1000);
                if(auditDeadline.before(new Date()))
                {
                    quartzExecute.taskMapper.autoUpdate(taskMaster.getTaskID(), TaskStatusEnum.AUDITED.getCode(),TaskStatusEnum.NEW.getCode());
                }
                else
                {
                    String jobName = "auditDeadlineJob|"+taskMaster.getTaskID();
                    String jobGroup ="auditDeadlineGroup";
                    String triggerName = "auditDeadlineTrigger|"+taskMaster.getTaskID();
                    String triggerGroup = "auditDeadlineTriggerGroup";
                    jobDataMap.put("executeDate",auditDeadline);
                    jobDataMap.put("taskID",taskMaster.getTaskID());
                    QuartzUtils.quartzConfig(jobName,jobGroup,jobDataMap,triggerName,triggerGroup,task);
                }
            }
        }

    }

    public static void taskExpiredQuartz(String task) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        List<TaskMaster> expiredTaskTaskMasters = quartzExecute.taskMapper.selectActiveTask();
        Date expiredDate = new Date();
        if(expiredTaskTaskMasters.size()>0){
            for(TaskMaster taskMaster: expiredTaskTaskMasters){
                expiredDate.setTime(taskMaster.getEndTime().getTime());
                if(expiredDate.before(new Date()))
                {
                    quartzExecute.taskMapper.autoUpdate(taskMaster.getTaskID(),TaskStatusEnum.EXPIRED.getCode(),TaskStatusEnum.AUDITED.getCode());
                }
                else
                {
                    String jobName = "taskExpiredJob|"+taskMaster.getTaskID();
                    String jobGroup ="taskExpiredGroup";
                    String triggerName = "taskExpiredTrigger|"+taskMaster.getTaskID();
                    String triggerGroup = "taskExpiredTriggerGroup";
                    jobDataMap.put("executeDate",expiredDate);
                    jobDataMap.put("taskID",taskMaster.getTaskID());
                    QuartzUtils.quartzConfig(jobName,jobGroup,jobDataMap,triggerName,triggerGroup,task);
                }
            }
        }
    }


    public static void taskCommitQuartz(String task) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        List<StepDTO> stepDTOS = quartzExecute.taskMapper.selectAcceptedTask();
        if(stepDTOS.size()>0){
            for(StepDTO stepDTO: stepDTOS){
                if(stepDTO.getEndTime().before(new Date()))
                {
                    quartzExecute.taskMapper.autoUpdate(stepDTO.getTaskID(),TaskStatusEnum.EXPIRED.getCode(),TaskStatusEnum.AUDITED.getCode());
                }
                else
                {
                    String jobName = "taskCommitJob|"+stepDTO.getTaskID();
                    String jobGroup ="taskCommitGroup";
                    String triggerName = "taskCommitTrigger|"+stepDTO.getTaskID();
                    String triggerGroup = "taskCommitTriggerGroup";
                    jobDataMap.put("executeDate",stepDTO.getEndTime());
                    jobDataMap.put("taskID",stepDTO.getTaskID());
                    QuartzUtils.quartzConfig(jobName,jobGroup,jobDataMap,triggerName,triggerGroup,task);
                }
            }
        }
    }



}
